package com.sample.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Message;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import com.base.sampleproject.R;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by devnn on 16/1/25.
 */
public class LukyDrawView extends View implements Runnable, Handler.Callback {
    private String TAG=this.getClass().getSimpleName();
    private Context context;
    private Paint paint;
    private Rect textBound;
    private Thread thread;
    private boolean flag;
    private List<String> prizes = new ArrayList<String>();
    private Handler handler = new Handler(this);
    private int j = 0;
    private DecimalFormat format=new DecimalFormat("00");

    public LukyDrawView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        paint = new Paint();
        textBound = new Rect();
        for (int i = 1; i <= 50; i++) {
            prizes.add("奖品" + format.format(i));
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        paint.setTextSize(100);
        paint.setTextScaleX(1);
        paint.getTextBounds(prizes.get(prizes.size()-1), 0, prizes.get(prizes.size()-1).length(), textBound);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(ContextCompat.getColor(context, R.color.color_deep_green));
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.STROKE);
        canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.LEFT);
        for (int i = 0; i < 10; i++) {
            if (i == 3) {
                paint.setColor(ContextCompat.getColor(context, R.color.color_red));
            } else {
                paint.setColor(ContextCompat.getColor(context, R.color.color_black));
            }
            canvas.drawText(prizes.get(j), getWidth() / 2 - textBound.width()/2,i * 120+90, paint);
            j=(j+1)%prizes.size();
            Log.i(TAG,"last j="+j);
        }
    }

    public void start() {
        flag = true;
        thread = new Thread(this);
        thread.start();
    }

    public void stop() {
        flag = false;
    }
    public String getPrize(){
        Log.i(TAG,"j="+j);
        return  prizes.get((j+3)% prizes.size());
//        return  prizes.get(j-7);
    }

    @Override
    public void run() {
        try {
            while (flag) {
                j++;
                if (j == prizes.size()) {
                    j = 0;
                }
                handler.sendEmptyMessage(10);
                Thread.sleep(10);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean handleMessage(Message msg) {
        if (msg.what == 10) {
            invalidate();
        }
        return false;
    }
}
